package study02;

public class Java06_twoFind {
    public static void main(String[] args) {
//        二分查找法
        int[] nums = {1, 2, 3, 4, 5, 6, 7};

//        查询目标
        int targetNum = 5;
        int start = 0;
        int end = nums.length - 1;
        int middle = 0;

        while (start <= end) {
//            先分割数组
            middle = (start + end) / 2;
            if (nums[middle] > targetNum) {
//                数据在左边
                end = middle - 1;
            } else if (nums[middle] < targetNum) {
//                数据在右边
                start = middle + 1;
            } else  {
//                就是中间的值就跳出循环
                break;
            }
        }
        System.out.println("数据在数组的位置" + middle);
    }
}
